Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zustand

Package Overview
Dependencies
Maintainers
3
Versions
142
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand

🐻 Bear necessities for state management in React

  • 3.7.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.7M
increased by8.88%
Maintainers
3
Weekly downloads
 
Created

What is zustand?

Zustand is a state management library for React and other JavaScript applications. It provides a simple and flexible way to create and manage global state without the complexity of traditional solutions like Redux. Zustand uses a hook-based API to allow components to subscribe to state changes and define actions for updating the state.

What are zustand's main functionalities?

Creating a store

This code sample demonstrates how to create a store with Zustand. The store has a state with a 'fishes' property and an 'addFish' action to increment the number of fishes.

import create from 'zustand';

const useStore = create(set => ({
  fishes: 0,
  addFish: () => set(state => ({ fishes: state.fishes + 1 }))
}));

Subscribing to state changes

This code sample shows how a React component can subscribe to state changes. The 'FishCounter' component uses the 'useStore' hook to access the number of fishes from the store's state.

import React from 'react';
import useStore from './store';

function FishCounter() {
  const fishes = useStore(state => state.fishes);
  return <div>{fishes} fishes</div>;
}

Updating the state

This code sample illustrates how to update the state using an action defined in the store. The 'AddFishButton' component gets the 'addFish' action from the store and uses it as an onClick event handler.

import React from 'react';
import useStore from './store';

function AddFishButton() {
  const addFish = useStore(state => state.addFish);
  return <button onClick={addFish}>Add a fish</button>;
}

Other packages similar to zustand

Keywords

FAQs

Package last updated on 07 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc